<template>
    <basic-container>
        <avue-crud :data="data" :option="option" v-model="obj" :page="page"
                   @row-update="rowUpdate"
                   @refresh-change="refreshChange"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @current-change="currentChange"
                   @size-change="sizeChange" @on-load="onLoad"></avue-crud>
    </basic-container>
</template>
<script>
    import {getList, editItem} from '@/api/account/userAccount/invoiceScale.js'
    import {validateServiceName,validateNum} from '@/util/util'
    export function testExtractRate(rule, value, callback) {
        setTimeout(() => {
            if(!value && value!=0){
                callback(new Error('输入不可以为空'));
            }else if (!Number(value) && Number(value)!=0) {
                callback(new Error('所输入内容必须是0-1之间的数字'));
            } else {
                const re = /^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$/;
                const rsCheck = re.test(value);
                if (!rsCheck) {
                    callback(new Error('请输入0-1之间的数字(可精确到0.01)'));
                } else {
                    if(value<0 || value>1){
                        callback(new Error('所输入内容必须是0-1之间的数字'));
                    }
                    else{
                        callback();
                    }
                }
            }

        }, 0);
    }
    export default {
        data() {
            return {
                dicBuyer: [],
                dicSell: [],
                page: {
                    pageSize: 10,
                    currentPage: 1,
                    total: 0
                },
                obj: {},
                data: [],
                option: {
                    // expand: true,
                    addBtn:false,
                    title: '',
                    page: false,
                    align: 'center',
                    menuAlign: 'center',
                    labelWidth: 140,
                    viewBtn:false,
                    delBtn:false,
                    border: true,
                    menuWidth: 300,
                    treeProps: {
                        children: "childrenNodes"
                    },
                    column: [
                        {
                            label: '税务品类名称',
                            prop: 'className',
                            row: true,
                            searchLabelWidth:120,
                            disabled:true,
                            search:true
                        },
                        {
                            label: '税收分类编码',
                            prop: 'taxClassificationCode',
                            row: true,
                            rules: [
                                {
                                    required: true,
                                    validator: validateNum,
                                    trigger: 'blur'
                                },
                                {max:19,min: 19,trigger:'blur',message:'请输入19位的税收分类编码'}
                            ]
                        },
                        {
                            label: '货物和劳务名称',
                            prop: 'nameOfService',
                            row: true,
                            rules: [
                                {
                                    required: true,
                                    message:'请输入货物和劳务名称',
                                    trigger: 'blur'
                                },
                                {validator: validateServiceName,trigger:'blur'}
                            ]
                        },
                        {
                            label: '一般税率(%)',
                            prop: 'generalTaxRate',
							type:'select',
							dicUrl:'base/goodsClass/taxRateList',
							props:{
								value:'code',
								label:'code'
							},
                            row: true,
                            rules: [
                                {
                                    required: true,
                                    trigger: 'blur',
                                    message:'请选择一般税率(%)'
                                }
                            ]
                        },
                        {
                            label: '小规模税率(%)',
                            prop: 'smallScaleTaxRate',
                            row: true,
							type:'select',
							dicUrl:'base/goodsClass/taxRateList',
							props:{
								value:'code',
								label:'code'
							},
                            rules: [
                                {
                                    required: true,
                                    trigger: 'blur',
                                    message:'请选择小规模税率(%)'
                                }
                            ]
                        },
                        {
                            label: '特殊地区税率(%)',
                            prop: 'specialTaxRate',
                            row: true,
							type:'select',
							props:{
								value:'code',
								label:'code'
							},
							dicUrl:'base/goodsClass/taxRateList',
                            rules: [
                                {
                                    required: true,
                                    trigger: 'blur',
                                    message:'请选择特殊地区税率(%)'
                                }
                            ]
                        },
                    ]
                }
            }
        },
        created() {
        },
        methods: {
            // 点击页数
            currentChange(currentPage) {
                this.page.currentPage = currentPage;
            },
            // 点击页数
            sizeChange(pageSize) {
                this.page.pageSize = pageSize;
            },
            onLoad(page, params = {}, done) {//页面初始化请求管理员列表
                getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                    if (done) {
                        done();
                    }
                    this.data = res.data.data
                    this.page.total = res.data.count
                })
            },
            searchReset() {
                this.query = {};
                this.onLoad(this.page);
            },
            // 搜索框
            searchChange(params, done) {
                this.page.currentPage = 1
                this.query = params;
                this.onLoad(this.page, params, done);
            },
            refreshChange({page, searchForm}) {
                this.onLoad(this.page);
            },
            // 编辑
            rowUpdate(row, index, done, loading) {
                console.log('row',row)
                console.log('obj',this.obj)
                this.editItemFn(row, done)
            },
            editItemFn(data, done) {
                let obj = {
                    addTime:data.addTime,
                    advPosEnc:data.advPosEnc,
                    classDisplay:data.classDisplay,
                    classLevel:data.classLevel,
                    className:data.className,
                    classSequence:data.classSequence,
                    deleteStatus:data.deleteStatus,
                    iconAccUrl:data.iconAccUrl,
                    id:data.id,
                    // invoiceRate:data.invoiceRate,
                    nodeId:data.nodeId,
                    nodeParentId:data.nodeParentId,
                    parentId:data.parentId,
                    percentageCommission:data.percentageCommission,
                    taxClassificationCode:data.taxClassificationCode,
                    nameOfService:data.nameOfService,
                    generalTaxRate:data.generalTaxRate,
                    smallScaleTaxRate:data.smallScaleTaxRate,
                    specialTaxRate:data.specialTaxRate
                }
                editItem(obj).then(res => {
                    if (res.data.code == 200) {
                        if (done) {
                            this.$message({
                                type: "success",
                                message: "修改成功"
                            });
                            this.onLoad(this.page)
                            done()
                        }
                    } else {
                        this.$message({
                            type: "error",
                            message: res.data.message
                        });
                        done()
                    }
                })
            },
        },
    }
</script>
<style lang="scss">

</style>